package com.chuku.mapper;

import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.bean.*;
import org.apache.ibatis.annotations.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author wxrob
 */

public interface ChuKuMapper {
    //出库
    @Select("select * from wms_t_customer where custName like ('${value}%')")
    public List<WmsTCustomer> getCusMess(String shuRu);

    @Select("SELECT * FROM wms_t_bill_details WHERE billID IN (SELECT billID FROM wms_t_bill WHERE billType = '0' AND billState='3' AND customerID = #{value})")
    public List<WmsTBillDetails> selChuKu(String custID);

    //办理出库插入wms_t_bill表
    @Insert("insert into wms_t_bill (" +
            "customerID,billID,billDate,billTransactor,billNote,billFeeStore,billFeeOther,billPayable,billPaid,billType,billState,billIsDel) " +
            "values(" +
            "#{customerID},#{billID},#{billDate},#{billTransactor},#{billNote},#{billFeeStore},#{billFeeOther},#{billPayable},#{billPaid},'1','0','0')")
    public void inChuKu(WmsTBill wmsTBill);
    //办理出库插入账单详情表wms_t_bill_details表
    @Insert("INSERT INTO wms_t_bill_details (detailsID,billID,trayID,detailsGoodsName,detailsGoodsSpec,currentGoodsNum,detailsGoodsNum,detailsEditTime) VALUES(#{detailsID},#{billID},#{trayID},#{detailsGoodsName},#{detailsGoodsSpec},#{currentGoodsNum},#{detailsGoodsNum},#{detailsEditTime})")
    public void inDetails(WmsTBillDetails wmsTBillDetails);

    //办理出库更改WmsTTray表,
    @Update("update wms_t_tray set trayState = 0 where trayID = #{trayID}")
    public void update(String trayID);

    //查询仓库不为空的仓库
    @Select("select * from wms_t_store where wms_t_store.storeID in(\n" +
            "select storeID from wms_t_site WHERE wms_t_site.siteID in(\n" +
            "select siteID from wms_t_tray where wms_t_tray.trayState = '1'));")
    public List<WmsTStore> selectStore();


    //查询仓库内容Stores
    @Select(" select  wms_t_bill.billID,wms_t_customer.custName,count(*) as count,wms_t_store.storeName,wms_t_bill.billDate,wms_t_bill.billNote,wms_t_bill.billIsDel from wms_t_bill\n" +
            "     LEFT JOIN wms_t_bill_details on wms_t_bill.billID = wms_t_bill_details.billID\n" +
            "     LEFT JOIN wms_t_tray on wms_t_tray.trayID = wms_t_bill_details.trayID\n" +
            "     LEFT JOIN wms_t_site on wms_t_site.siteID = wms_t_tray.siteID\n" +
            "     LEFT JOIN wms_t_store on wms_t_site.storeID = wms_t_store.storeID\n" +
            "     LEFT JOIN wms_t_customer on wms_t_bill.customerID = wms_t_customer.custID\n" +
            "     where wms_t_store.storeID= #{value} group by wms_t_bill_details.billID")
    public List<Map<String, Object>> selStores(String storeID);


    //出库单查询
    @Select(" <script>\n" +
            "        select wms_t_bill.*,wms_t_customer.custName from wms_t_bill LEFT JOIN wms_t_customer on wms_t_bill.customerID =\n" +
            "        wms_t_customer.custID WHERE billType=1 and billState=0 and billIsDel=0\n" +
            "        <if test=\"custName != null and custName != ''\">\n" +
            "            and wms_t_bill.customerID in(SELECT custID FROM wms_t_customer WHERE wms_t_customer.custName = #{custName})\n" +
            "        </if>\n" +
            "        <if test=\"start != null and start != ''\">\n" +
            "            <if test=\"end != null and end != ''\">\n" +
            "                and wms_t_bill.billDate BETWEEN #{start} AND #{end}\n" +
            "            </if>\n" +
            "        </if>\n" +
            "        <if test=\"billID != null and billID != ''\">\n" +
            "            and wms_t_bill.billID = #{billID}\n" +
            "        </if>\n" +
            "    </script>")
    public List<Map<String, Object>> bill(String start,String end,String custName,String billID);

    //出库单明细查询
    @Select("<script>\n" +
            "        SELECT wms_t_bill_details.*,wms_t_customer.custName from wms_t_bill_details\n" +
            "        left join wms_t_bill on wms_t_bill_details.billID = wms_t_bill.billID\n" +
            "        LEFT JOIN wms_t_customer on wms_t_bill.customerID = wms_t_customer.custID\n" +
            "        WHERE billType='1' and billState='0' and billIsDel='0'\n" +
            "        <if test=\"custName != null and custName != ''\">\n" +
            "            and wms_t_bill_details.billID in (select wms_t_bill.billID from wms_t_bill where customerID in(SELECT custID FROM wms_t_customer WHERE wms_t_customer.custName = #{custName})))\n" +
            "        </if>\n" +
            "        <if test=\"start != null and start != ''\">\n" +
            "            <if test=\"end != null and end != ''\">\n" +
            "                and wms_t_bill_details.detailsEditTime BETWEEN #{start} AND #{end}\n" +
            "            </if>\n" +
            "        </if>\n" +
            "        <if test=\"billID != null and billID != ''\">\n" +
            "            and wms_t_bill.billID = #{billID};\n" +
            "        </if>\n" +
            "    </script>")
    public List<Map<String, Object>> details(String start,String end,String custName,String billID);

}
